package org.jeecgframework.tag.core.easyui;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import jodd.util.StringUtil;

import org.jeecgframework.core.enums.SysThemesEnum;
import org.jeecgframework.core.util.SysThemesUtil;
import org.jeecgframework.core.util.oConvertUtils;

/**
 * 
 * @author zt
 * 
 */
public class BaseTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	protected String type = "default";// 加载类型
	protected String cssTheme;

	public String getCssTheme() {
		return cssTheme;
	}

	public void setCssTheme(String cssTheme) {
		this.cssTheme = cssTheme;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			// update-start--Author:longjb Date:20150408 for：手动设置指定属性主题优先
			if (cssTheme == null) {// 
				Cookie[] cookies = ((HttpServletRequest) super.pageContext
						.getRequest()).getCookies();
				for (Cookie cookie : cookies) {
					if (cookie == null || StringUtil.isEmpty(cookie.getName())) {
						continue;
					}
					if (cookie.getName().equalsIgnoreCase("JEECGCSSTHEME")) {
						cssTheme = cookie.getValue();
					}
				}
			}
			if (cssTheme == null || "".equals(cssTheme)) {
				cssTheme = "ace";
			}
			SysThemesEnum sysThemesEnum = SysThemesUtil
					.getSysTheme((HttpServletRequest) super.pageContext
							.getRequest());
			String types[] = type.split(",");

			// 插入多语言脚本
			String lang = (String) ((HttpServletRequest) this.pageContext
					.getRequest()).getSession().getAttribute("lang");
			String langjs = StringUtil
					.replace(
							"<script type=\"text/javascript\" src=\"plug-in/mutiLang/{0}.js\"></script>",
							"{0}", lang);
			sb.append(langjs);

			if (oConvertUtils.isIn("jquery-webos", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/sliding/js/jquery-1.7.1.min.js\"></script>");
			} else if (oConvertUtils.isIn("jquery", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/jquery/jquery-1.8.3.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/jquery/jquery.cookie.js\" ></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/jquery-plugs/storage/jquery.storageapi.min.js\" ></script>");
			}
			if (oConvertUtils.isIn("ckeditor", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/ckeditor/ckeditor.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/ckeditorTool.js\"></script>");
			}
			if (oConvertUtils.isIn("ckfinder", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/ckfinder/ckfinder.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/ckfinderTool.js\"></script>");
			}
			if (oConvertUtils.isIn("easyui", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/dataformat.js\"></script>");
				// sb.append("<link id=\"easyuiTheme\" rel=\"stylesheet\" href=\"plug-in/easyui/themes/"+cssTheme+"/easyui.css\" type=\"text/css\"></link>");
				sb.append(SysThemesUtil.getEasyUiTheme(sysThemesEnum));
				sb
						.append("<link rel=\"stylesheet\" href=\"plug-in/easyui/themes/icon.css\" type=\"text/css\"></link>");
				sb
						.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/accordion/css/accordion.css\">");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/easyui/jquery.easyui.min.1.3.2.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/easyui/locale/zh-cn.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/syUtil.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/easyui/extends/datagrid-scrollview.js\"></script>");
			}
			if (oConvertUtils.isIn("DatePicker", types)) {
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/My97DatePicker/WdatePicker.js\"></script>");
			}

			if (oConvertUtils.isIn("tools", types)) {
				// sb.append("<link rel=\"stylesheet\" href=\"plug-in/tools/css/"+("metro".equals(cssTheme)?"metro/":"")+"common.css\" type=\"text/css\"></link>");
				sb.append(SysThemesUtil.getCommonTheme(sysThemesEnum));
				// sb.append("<script type=\"text/javascript\" src=\"plug-in/lhgDialog/lhgdialog.min.js"+("metro".equals(cssTheme)?"?skin=metro":"")+"\"></script>");
				sb.append(SysThemesUtil.getLhgdialogTheme(sysThemesEnum));
				sb
						.append(StringUtil
								.replace(
										"<script type=\"text/javascript\" src=\"plug-in/tools/curdtools_{0}.js\"></script>",
										"{0}", lang));

				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/easyuiextend.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/jquery-plugs/hftable/jquery-hftable.js\"></script>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/tools/json2.js\" ></script>");
			}
			if (oConvertUtils.isIn("toptip", types)) {
				sb
						.append("<link rel=\"stylesheet\" href=\"plug-in/toptip/css/css.css\" type=\"text/css\"></link>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/toptip/manhua_msgTips.js\"></script>");
			}
			if (oConvertUtils.isIn("autocomplete", types)) {
				sb
						.append("<link rel=\"stylesheet\" href=\"plug-in/jquery/jquery-autocomplete/jquery.autocomplete.css\" type=\"text/css\"></link>");
				sb
						.append("<script type=\"text/javascript\" src=\"plug-in/jquery/jquery-autocomplete/jquery.autocomplete.min.js\"></script>");
			}
			if (oConvertUtils.isIn("css-layout", types)) {
				
				sb
						.append("<link rel=\"stylesheet\"  href=\"plug-in/css-layout/css/css-layout.css\" />");
				sb
				.append("<link rel=\"stylesheet\"  href=\"plug-in/css-layout/css/css-ui.css\" />");
				
				sb
						.append("<script src=\"plug-in/css-layout/js/global.js\"></script>");
			}
			if (oConvertUtils.isIn("ace-style", types)) {
				sb
						.append("<link rel=\"stylesheet\" href=\"plug-in/ace/css/ace.css?3\" />");
				sb
						.append("<link rel=\"stylesheet\" href=\"plug-in/ace/css/font-awesome.css?3\" />");
				
			}
			if (oConvertUtils.isIn("echarts", types)) {
				sb
				.append("<script src=\"plug-in/css-layout/js/echarts.js\"></script>");
				
			}

			out.print(sb.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

}
